package com.fwtai.controller.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.service.web.MallGoodsService;
import com.fwtai.tool.ToolClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 产品（商品）控制层|路由层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2023-10-31 15:22:53
 * @官网 <url>http://www.fwtai.com</url>
*/
@RestController
@RequestMapping("/mallGoods")
public class MallGoodsController{

  private final MallGoodsService mallGoodsService;

  public MallGoodsController(final MallGoodsService mallGoodsService){
    this.mallGoodsService = mallGoodsService;
  }

  /**添加*/
  @PreAuthorize("hasAuthority('mall_goods_btn_add')")
  @PostMapping("/add")
  public void add(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.add(request),response);
  }

  /**行内编辑*/
  @PreAuthorize("hasAuthority('mall_goods_row_edit')")
  @PostMapping("/edit")
  public void edit(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.edit(request),response);
  }

  /**根据id查询对应的数据*/
  @PreAuthorize("hasAuthority('mall_goods_row_queryById')")
  @GetMapping("/queryById")
  public void queryById(final Long id,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.queryById(id),response);
  }

  /**批量删除(超级管理员使用)*/
  @PreAuthorize("hasAuthority('mall_goods_btn_delByKeys')")
  @PostMapping("/delByKeys")
  public void delByKeys(final String ids,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.delByKeys(ids),response);
  }

  /**商品的上架或下架-单行*/
  @PreAuthorize("hasAuthority('mall_goods_row_valid')")
  @PostMapping("/editValid")
  public void editValid(final Long kid,final Integer enabled,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.editValid(kid,enabled),response);
  }

  /**获取商品产品规格数据*/
  @PreAuthorize("hasAuthority('mall_goods_row_listSpecs')")
  @GetMapping("/getGoodsSpecList")
  public void getGoodsSpecList(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.getGoodsSpecList(new PageFormData(request)),response);
  }

  /**获取数据*/
  @PreAuthorize("hasAuthority('mall_goods_btn_listData')")
  @GetMapping("/listData")
  public void listData(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.listData(new PageFormData(request)),response);
  }

  //获取表goods的字段category的下拉框列动态值选项的数据
  @PreAuthorize("hasAuthority('mall_goods_btn_category')")
  @GetMapping("/getCategory")
  public void getCategory(final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.getCategory(),response);
  }

  /**获取单个商品规格详情*/
  @PreAuthorize("hasAuthority('mall_goods_row_specInfo')")
  @GetMapping("/querySpecInfo")
  public void querySpecInfo(final Long id,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.querySpecInfo(id),response);
  }

  /**添加商品规格*/
  @PreAuthorize("hasAuthority('mall_goods_btn_addSpec')")
  @PostMapping("/addGoodSpec")
  public void addGoodSpec(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.addGoodSpec(request),response);
  }

  /**行内编辑商品规格*/
  @PreAuthorize("hasAuthority('mall_goods_row_editSpec')")
  @PostMapping("/editGoodSpec")
  public void editGoodSpec(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.editGoodSpec(request),response);
  }

  /**隐藏|显示商品规格*/
  @PreAuthorize("hasAuthority('mall_goods_row_specStatus')")
  @PostMapping("/editSpecStatus")
  public void editSpecStatus(final Long kid,final Integer enabled,final Long goodsId,final HttpServletResponse response){
    ToolClient.responseJson(mallGoodsService.editSpecStatus(kid,enabled,goodsId),response);
  }
}